package com.boe.csb.core.service.user;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.boe.csb.core.common.status.UserPicType;
import com.boe.csb.core.common.utils.FtpUtil;
import com.boe.csb.core.dao.BoeUserFtpCriteria;
import com.boe.csb.core.dao.BoeUserFtpMapper;
import com.boe.csb.core.entity.po.BoeUserFtp;

/**
 * Created by tianxiang.luo on 16/12/19.
 */
@Service(value = "boeUserFtpService")
public class BoeUserFtpService {


    @Autowired
    private BoeUserFtpMapper boeUserFtpMapper;

    public Long updateUserFtp(String path, String fileName, Long userId, String picType, Long csbApiId) {
        BoeUserFtp userFtp = new BoeUserFtp();
        userFtp.setFilepath(path);
        userFtp.setFilename(FtpUtil.getFtpFileName(fileName));
        userFtp.setCsbApiId(csbApiId);
        userFtp.setType(picType);
        BoeUserFtp cFtp = findPathByUserIdAndTypeAndCsbId(String.valueOf(userId), picType, csbApiId);
        if (cFtp == null) {
            userFtp.setUserid(userId);
            boeUserFtpMapper.insert(userFtp);
            return userFtp.getId();
        } else {
            userFtp.setId(cFtp.getId());
            boeUserFtpMapper.updateByPrimaryKeySelective(userFtp);
            return userFtp.getId();
        }
    }


    /**
     * 根据userId 和 type    api详情使用的
     */
    public BoeUserFtp findPathByUserIdAndType(String userId, String type, Long csbApiId) {
        BoeUserFtpCriteria ftpCriteria = new BoeUserFtpCriteria();
        BoeUserFtpCriteria.Criteria subCriteria = ftpCriteria.createCriteria();
        subCriteria.andUseridEqualTo(Long.valueOf(userId));
        if (type != null) {
            subCriteria.andTypeEqualTo(type);
        }
        if (csbApiId != null) {
            subCriteria.andCsbApiIdEqualTo(csbApiId);
        }
        ftpCriteria.setOrderByClause("id desc");
        List<BoeUserFtp> ftpList = boeUserFtpMapper.selectByExample(ftpCriteria);
        if (CollectionUtils.isEmpty(ftpList) && UserPicType.businessLicense.name().equals(type)) {
            ftpCriteria.clear();
            ftpCriteria.createCriteria().andTypeEqualTo(UserPicType.idCard.name()).andUseridEqualTo(Long.valueOf(userId));
            ftpList = boeUserFtpMapper.selectByExample(ftpCriteria);
            if (!CollectionUtils.isEmpty(ftpList))
                return ftpList.get(0);
        }
        if(CollectionUtils.isEmpty(ftpList) && UserPicType.servicePic.name().equals(type)){
            ftpCriteria.clear();
            ftpCriteria.createCriteria().andTypeEqualTo(UserPicType.companyLogo.name()).andUseridEqualTo(Long.valueOf(userId));
            ftpList = boeUserFtpMapper.selectByExample(ftpCriteria);
            if (!CollectionUtils.isEmpty(ftpList))
                return ftpList.get(0);
        }
        if (!CollectionUtils.isEmpty(ftpList))
            return ftpList.get(0);
        else
            return null;
    }
    
    /**
     * 根据id 和 type 读取图片
     */
    public BoeUserFtp findPathByAppPic(Long id,String type) {
        BoeUserFtpCriteria ftpCriteria = new BoeUserFtpCriteria();
        BoeUserFtpCriteria.Criteria subCriteria = ftpCriteria.createCriteria();
        subCriteria.andIdEqualTo(id);
        if (type != null) {
        	subCriteria.andTypeEqualTo(type);
        }
        ftpCriteria.setOrderByClause("id desc");
        List<BoeUserFtp> ftpList = boeUserFtpMapper.selectByExample(ftpCriteria);      
        if (!CollectionUtils.isEmpty(ftpList))
            return ftpList.get(0);
        else
            return null;
    }

    /**
     * 根据userId 和 type csbId   用户使用的
     */
    public BoeUserFtp findPathByUserIdAndTypeAndCsbId(String userId, String type, Long csbApiId) {
        BoeUserFtpCriteria ftpCriteria = new BoeUserFtpCriteria();
        BoeUserFtpCriteria.Criteria subCriteria = ftpCriteria.createCriteria();
        subCriteria.andUseridEqualTo(Long.valueOf(userId));
        if (type != null) {
            subCriteria.andTypeEqualTo(type);
        }
        if (csbApiId != null) {
            subCriteria.andCsbApiIdEqualTo(csbApiId);
        }
        List<BoeUserFtp> ftpList = boeUserFtpMapper.selectByExample(ftpCriteria);
        if (!CollectionUtils.isEmpty(ftpList))
            return ftpList.get(0);
        else
            return null;
    }



    public Long insertUserFtp(Long userId, String filePath, String fileName, String type, Long csbApiId) {
        BoeUserFtp ftp = new BoeUserFtp();
        ftp.setCsbApiId(csbApiId);
        ftp.setType(type);
        ftp.setFilename(fileName);
        ftp.setFilepath(filePath);
        ftp.setUserid(userId);
        boeUserFtpMapper.insert(ftp);
        return ftp.getId();
    }


}
